﻿package utils.common.time {
	import flash.utils.getTimer;
	
	/**
	 * // usage example:
	 * private var _PlayTime:Number = 0;
	 * private var _time:AccurateTimer = new AccurateTimer();
	 * 
	 * addEventListener( Event.ENTER_FRAME, UpdateMain );
	 * 
	 * private function UpdateMain( e:Event ) { // Update delta time
			_time.Update();
			Update( _time.getDT() );
	   }
	   
	   private function Update( dt:Number ):void {
			_PlayTime += dt;  // Value inc every second
			trace(" _PlayTime ");
		}
	 * 
	 * ...
	 * Timer utility with accuracy
	 * @author Aleksey Trufanov
	 */
	public class AccurateTimer{
		private var dt:Number; // delta time
		private var CurrentTime:int;
		private var LastTime:int;
		
		/**
		 * Constructor
		 */
		public function AccurateTimer(){
			dt = 0.0;
			LastTime = 0;
			CurrentTime = getTimer(); // Get current system time
		}
		
		/**
		 * Stop
		 */
		public function StopTimer():void {
			dt = 0.0;
			CurrentTime = 0;
			LastTime = 0;
		}
		
		/**
		 * Resume
		 */
		public function RunTimer():void {
			dt = 0.0;
			CurrentTime = getTimer(); // Get current system time
			LastTime = getTimer(); // Get current system time
		}
		
		/**
		 * Call this from class where instance of AccurateTimer. e.g. from ENTER_FRAME
		 */
		public function Update() {
			CurrentTime = getTimer();
			dt = ( CurrentTime - LastTime ) / 1000;
			LastTime = CurrentTime;
		}
		
		/**
		 * Get accuracy delta
		 * @return
		 */
		public function getDT():Number {
			return dt;
		}
		
	}
}